package com.itheima.stock.controller;

import com.itheima.stock.pojo.vo.req.AddPermissionVo;
import com.itheima.stock.pojo.entity.SysPermission;
import com.itheima.stock.pojo.vo.req.EditPermissionVo;
import com.itheima.stock.pojo.vo.resp.R;
import com.itheima.stock.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class PermissionsController {

    @Autowired
    private PermissionsService permissionsService;
    @PreAuthorize("hasAuthority('sys:permission:list')")
    @GetMapping("/permissions")
    public R<List<SysPermission>> getPermissions(){
        return permissionsService.getPermissions();
    }
    @GetMapping("/permissions/tree")
    public R<List<Map>> getPermissionsTree(){
        return permissionsService.getPermissionsTree();
    }
    @PreAuthorize("hasAuthority('sys:permission:add')")
    @PostMapping("/permission")
    public R addPermission(@RequestBody AddPermissionVo addPermissionVo){
        return permissionsService.addPermission(addPermissionVo);
    }
    @PreAuthorize("hasAuthority('sys:permission:update')")
    @PutMapping("/permission")
    public R editPermission(@RequestBody EditPermissionVo editPermissionVo){
        return permissionsService.editPermission(editPermissionVo);
    }
    @PreAuthorize("hasAuthority('sys:permission:delete')")
    @DeleteMapping("/permission/{permissionId}")
    public R delPermission(@PathVariable String permissionId){
        return permissionsService.delPermission(permissionId);
    }

}
